import java.util.Stack;

public class Test {

    public static boolean isValid(String s) {
        Stack<Character> characters=new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char getc=s.charAt(i);
            if (getc=='('||getc=='['||getc=='{'){
                characters.push(getc);
            }else {
                if (characters.empty())
                    return false;
                char stop=characters.peek();
                if (stop=='('&&getc==')'||stop=='['&&getc==']'||stop=='{'&&getc=='}'){
                    characters.pop();
                }else {
                    return false;
                }
            }
        }
        return characters.empty()?true:false;
    }

    //---------------------------------------------------------
    public static boolean isFu(String s){
        if(s.equals("+")||s.equals("-")||s.equals("*")||s.equals("/"))
            return true;
        return false;
    }


    public static int count(int num1,int num2,String st){
        int ret=0;
        switch (st){
            case "+":
                ret=num1+num2;
                break;
            case "-":
                ret=num1-num2;
                break;
            case "*":
                ret=num1*num2;
                break;
            case "/":
                ret=num1/num2;
                break;
        }
        return ret;
    }



    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if(isFu(tokens[i])){
                int num2=stack.pop();
                int num1=stack.pop();
                stack.push(count(num1,num2,tokens[i]));
            }else {
                stack.push(Integer.parseInt(tokens[i]));
            }
        }
        return stack.peek();
    }





    public static void main(String[] args) {
//        String st="()[]{}";
//        System.out.println(isValid(st));

        String[] tokens = {"2","1","+","3","*"};
        System.out.println(evalRPN(tokens));
    }

}
